/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


function loadNbEpisode(saison, episode) {
    $.ajax({
        url: 'src/pdo/timeline.php',
        type: 'POST',
        data: {function: "loadNbEpisode", saison: saison, episode:episode},
        success: function (data)
        {
            $("#episode").html(remplirSelectEpisode(data, episode));
            $("#circles-slider").remove();
            createTimeline(data, episode);
        }

    });
}

function remplirSelectEpisode(nbEpisode, episode) {
    var selectList = "";
    for (var i = 1; i <= nbEpisode; i++) {
        if(i == episode){
            selectList += "<option selected='selected' value=" + i + ">" + i + "</option>";
        }
        else{
            selectList += "<option value=" + i + ">" + i + "</option>";
        }
        
    }
    return selectList;
}

function createTimeline(maxEp, episode) {
    var zone = $("#timelineSelect");
    var select = $("#episode");
//    maxEp = parseInt(maxEp) +1;
    var slider = $("<div id='circles-slider'></div>").insertAfter(zone).slider({
        min: 1,
        max: maxEp,
        range: "min",
        value: episode,
        slide: function (event, ui) {
            select[ 0 ].selectedIndex = ui.value - 1;
            changeEpisode(ui.value);
        }
    }).slider("pips", {
        rest: "label"
    });
}

function changeSaison(saison) {
    var newSaison = parseInt(saison);
    var lastSaison = parseInt($("#saison").children().last().val());
    var slider = $("#circles-slider");

    $("#saison").val(newSaison);
    $("#nbSaison").html(newSaison);
    //gestion des episodes
    slider.slider("value", 1);
    $("#episode").val(1);
    loadNbEpisode(newSaison, 1);
    if (newSaison === 1) {
        $("#timeLeft").hide();
    }
    else {
        $("#timeLeft").show();
    }
    if (newSaison === lastSaison) {
        $("#timeRight").hide();
    }
    else {
        $("#timeRight").show();
    }
}

function saveTimeline(saison, episode) {
    $.ajax({
        url: 'src/pdo/timeline.php',
        type: 'POST',
        data: {function: "saveTimeline", saison: saison, episode: episode},
        success: function (data)
        {
            $(".timeline").html(data);
            $("#timelineMessage").html("<span>Sauvegarde réussie</span>");
            $("#timelineMessage span").fadeOut(4000, function () {
                $(this).remove();
            });

        }

    });
}

//chargement de la timeline connecté ou non
function loadTimeline(saison, episode){
    $("#saison").val(saison);
    $("#nbSaison").html(saison);
    var slider = $("#circles-slider");
    var lastSaison = parseInt($("#saison").children().last().val());
    //gestion des episodes
    loadNbEpisode(saison, episode);
    $("#episode").val(episode);
    slider.slider("value", episode);
    //gestion des fleches saison
    if (saison === 1) {
        $("#timeLeft").hide();
    }
    else {
        $("#timeLeft").show();
    }
    if (saison === lastSaison) {
        $("#timeRight").hide();
    }
    else {
        $("#timeRight").show();
    }
}

//changement de l'episode change la variable session
function changeEpisode(episode){
    $.ajax({
        url: 'src/pdo/timeline.php',
        type: 'POST',
        data: {function: "changeEpisode", episode:episode},
        success: function (data)
        {
            
        }

    });
}